﻿<div id="scan" style="z-index:10000;border:1px solid #9a9a9a;display:none;">
    <div id="title" style="width:100%;height:30px;font-size:14pt;background-color:#e5e5e5;padding-top:10px;"><span style="margin-left:15px;margin-top:25px;color:#9a9a9a;">商品扫描</span></div>
    <div style="background-color:white;width:100%;height:260px;">
        <table style="margin-left:15px;">
                <tr style="height:20px;"><td colspan="3"></td></tr>
                <tr style="height:50px;">
                    <td width ="68px" align="right">条码：</td>
                    <td width ="200px" align="left" colspan="2">
                        <input id="FCode" name="FCode" class="mini-textbox" style="width:100%;"  onenter="enter(1)"/> 
                    </td>
                    <td></td>
                </tr>
                <tr style="height:50px;">
                    <td width ="68px" align="right">名称：</td>
                    <td width ="200px" align="left" colspan="2">
                        <input id="FName" name="FName" class="mini-textbox" readOnly="true" onended ="false" style="width:100%;"/> 
                    </td>
                    <td></td>
                </tr>
                <tr style="height:50px;">
                    <td width ="68px"  align="right">数量：</td>
                    <td width ="190px" align="left" colspan="2">
                        <input id ="FCount" name="FCount" class="mini-spinner"style="width:100%;" value="1" maxValue="99999900"   showbutton="false"  changeOnMousewheel ="false"
                              onenter="enter(2)"/> 
                    </td>
                    <td>
                        <div id="FDefaultCount" name="FDefaultCount" class="mini-checkbox" readOnly="false" text="默认数量" onvaluechanged="setDefaultCount" style="margin-left:10px;"></div>
                    </td>
                </tr>
                <tr style="height:50px;">
                    <td width ="70px"  align="right">单价：</td>
                    <td width ="150px" align="left" colspan="2">
                        <input id ="FPrice" name="FPrice" class="mini-spinner" style="width:100%;" value="0"  maxValue="999999999"  showbutton="false" 
                            changeOnMousewheel ="false"  onenter="enter(3)"/>
                    </td>
                     <td>
                        <div id="FDefaultPrice" name="FDefaultPrice" class="mini-checkbox" readOnly="false" text="默认单价" onvaluechanged="setDefaultPrice" style="margin-left:10px;"></div>
                    </td>
               </tr>
                <tr style="height:50px;">
                    <td></td>
                    <td>
                        <a class="mini-button"   onclick="enter(3)" style="font-size:12pt;height:25px;width:80px;">添加</a>
                    </td>
                    <td>
                        <a class="mini-button"   onclick="Close()" style="font-size:12pt;height:25px;width:80px;">关闭</a>
                    </td>
               </tr>
       </table>
   </div>
</div>
<div class="window-mask" style="z-index:9999;"></div>

<script>
    var getRow = {};
    var timer1;
    var scan_stockID = 0;

    $(function () {
        mini.getbyName("FCode").focus();
        mini.getbyName("FCount").setValue(1);
    });

    function enter(ctrl) {
        if (ctrl == 1) {
            getRow = {};
            scanGoods(mini.getbyName("FCode").getValue());
            if (mini.getbyName("FDefaultCount").getValue() == "false") {
                mini.getbyName("FCount").focus();
                mini.getbyName("FCount").selectText();
            }
            else if (mini.getbyName("FDefaultPrice").getValue() == "false") {
                mini.getbyName("FPrice").focus();
                mini.getbyName("FPrice").selectText();
            }
            else
                scanAddRow();
        } else if (ctrl == 2) {
            if (mini.getbyName("FDefaultPrice").getValue() == "false") {
                mini.getbyName("FPrice").focus();
                mini.getbyName("FPrice").selectText();
            }
            else
                AddRow();
        } else if (ctrl == 3) {
            AddRow();
        }
    };

    function Extra(obj) {
        try {
            if (typeof (ExtraFunc) == "function") {
                return ExtraFunc(obj);
            }
            else {
                return obj;
            }
        } catch (e) {
            return obj;
        }
    }

    function AddnewRow() {
        getRow.mFPrice = mini.getbyName("FPrice").getValue();
        getRow.mFQty = mini.getbyName("FCount").getValue();
        getRow.mFAmount = getRow.mFPrice * getRow.mFQty;
        getRow.mFPurAmount = getRow.mFPurPrice * getRow.mFQty;
        getRow.mFPosAmount = getRow.mFPosPrice * getRow.mFQty;
        var newRow = Extra(getRow);
        grid.addRow(newRow, grid.getData().length);
        mini.getbyName("FCode").focus();
        mini.getbyName("FCode").selectText();
        getRow = {};
        BillState(4);
        mini.getbyName("FCode").setEnabled(true);
        if (mini.getbyName("FDefaultPrice").getValue() == "false") {
            mini.getbyName("FPrice").setEnabled(true);
        }
        if (mini.getbyName("FDefaultCount").getValue() == "false") {
            mini.getbyName("FCount").setEnabled(true);
        }
    }

    function AddRow() {
        if (!getRow.hasOwnProperty('mFItemID')) {
            mini.getbyName("FCode").focus();
            return;
        }
        var rCnt = grid.getData().length;
        if (rCnt > 0) {
            var rec = grid.getRow(rCnt - 1);
            if (rec.mFItemID == undefined) {
                grid.removeRow(rec, false);
            }
            AddnewRow();
        }
        else
            AddnewRow();
        mini.getbyName("FCode").setValue("");
        mini.getbyName("FPrice").setValue(0);
        getRow = {};
    }

    function scanAddRow() {
        if (getRow.hasOwnProperty('mFItemID')) {
            AddRow();
        }
    }

    function scanGoods(txtValue) {
        var type = "0";
        type = datatype;
        if (datatype == "10" || datatype == "11" || datatype == "12") type = 2;

        var FObjectID = 0;
        if (datatype == 1) {
            scan_stockID = mini.getbyName("FStockID").getValue();
            FObjectID = mini.getbyName("FSupplyItemID").getValue();
        }
        else if (datatype == 2) {
            scan_stockID = mini.getbyName("FStockID").getValue();
            FObjectID = mini.getbyName("FCustItemID").getValue();
        }

        var json = "[{";
        json = json + "'FClassID':" + "'0',";
        json = json + "'FObjectID':" + "'" + FObjectID + "',";
        json = json + "'FBillType':" + "'" + datatype + "',";
        json = json + "'FSupplierID':" + "'-1',";
        json = json + "'FNumber':'" + txtValue + "',";
        json = json + "'FName':'" + "" + "',";
        json = json + "'FStockID':'" + scan_stockID + "',";
        json = json + "'FSystemAppID':'" + parent.window.getSystemAppID() + "'}]";
        if ((txtValue != null) && (txtValue != "")) {
            $.post("../Bill/GetBillGoodss", { datajson: json }, function (data) {
                var ret = mini.decode(data);
                if (ret.data.length == 1) {
                    var action = ret.data[0];
                    getRow = {
                        mFItemID: action.FItemID, mFNumber: action.FNumber, mFName: action.FName, mFModel: action.FSpec,
                        mFUnitName: action.FUnitName, mFQty: 1, mFPrice: action.FPrice, mFCode: action.FCode, mFAmount: 0,
                        mFPurPrice: action.FPurPrice, mFPurAmount: action.FPurPrice, mFPosPrice: action.FPosPrice, mFPosAmount: action.FPosPrice
                    };
                    mini.getbyName("FName").setValue(action.FName);
                    if (action.newPrice == undefined)
                        mini.getbyName("FPrice").setValue(action.FPrice);
                    else
                        mini.getbyName("FPrice").setValue(action.newPrice);
                    if (mini.getbyName("FDefaultCount").getValue() == "true" && mini.getbyName("FDefaultPrice").getValue() == "true")
                        scanAddRow();
                }
                else {
                    getRow = {};
                    scanGoodsWin(txtValue);
                }
            });
        }
        else {
            scanGoodsWin(txtValue);
        }
    }

    function scanGoodsWin(txtValue) {
        clearInterval(timer1);
        var FObjectID = 0;
        if (datatype == 1) {
            scan_stockID = mini.getbyName("FStockID").getValue();
            FObjectID = mini.getbyName("FSupplyItemID").getValue();
        }
        else if (datatype == 2) {
            scan_stockID = mini.getbyName("FStockID").getValue();
            FObjectID = mini.getbyName("FCustItemID").getValue();
        }

        mini.open({
            url: "../Bill/GetBillGoods",
            title: "选择商品", width: 900, height: 500,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { key: txtValue, mBillType: datatype, mStockID: scan_stockID, FObjectID: FObjectID };
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
                if (action) {
                    var rw = action[0];
                    if (rw != undefined) {
                        getRow = {
                            mFItemID: rw.FItemID, mFNumber: rw.FNumber, mFName: rw.FName, mFModel: rw.FSpec,
                            mFUnitName: rw.FUnitName, mFQty: 1, mFPrice: rw.FPrice, mFCode: rw.FCode, mFAmount: 0,
                            mFPurPrice: rw.FPurPrice, mFPurAmount: rw.FPurPrice, mFPosPrice: rw.FPosPrice, mFPosAmount: rw.FPosPrice
                        };
                        if (rw.newPrice == undefined)
                            mini.getbyName("FPrice").setValue(rw.FPrice);
                        else
                            mini.getbyName("FPrice").setValue(rw.newPrice);
                        mini.getbyName("FCode").setValue(rw.FCode);
                        if (mini.getbyName("FDefaultCount").getValue() == "false") {
                            mini.getbyName("FCount").focus();
                        }
                        else if (mini.getbyName("FDefaultPrice").getValue() == "false") {
                            mini.getbyName("FPrice").focus();
                        }
                        else if (mini.getbyName("FDefaultCount").getValue() == "true" && mini.getbyName("FDefaultPrice").getValue() == "true")
                            scanAddRow();
                    }
                }
            }
        });
    }

    function setDefaultCount() {
        if (mini.getbyName("FDefaultCount").getValue() == "true")
            mini.getbyName("FCount").setEnabled(false);
        else
            mini.getbyName("FCount").setEnabled(true);
        mini.getbyName("FCode").setValue("");
    }

    function setDefaultPrice() {
        if (mini.getbyName("FDefaultPrice").getValue() == "true")
            mini.getbyName("FPrice").setEnabled(false);
        else
            mini.getbyName("FPrice").setEnabled(true);

        if (mini.getbyName("FDefaultCount").getValue() == "true" && mini.getbyName("FDefaultPrice").getValue() == "true") {
            mini.getbyName("FPrice").setEnabled(false);
        }
        mini.getbyName("FCode").setValue("");
    }


    //2017-11-17更新
    function InitScan() {
        var iWidth = 400; //弹出窗口的宽度;
        var iHeight = 300; //弹出窗口的高度;
        var iTop = (document.documentElement.clientHeight - iHeight) / 2;
        var iLeft = (document.documentElement.clientWidth - iWidth) / 2;
        $("#scan").height(iHeight)
            .width(iWidth)
            .css('position', 'absolute')
            .css("top", iTop)
            .css("left", iLeft);
    }

    function ScanAdd() {
        mini.getbyName("FCode").setValue("");
        mini.getbyName("FCount").setValue("1");
        mini.getbyName("FPrice").setValue("0");
        var iWidth = 400; //弹出窗口的宽度;
        var iHeight = 300; //弹出窗口的高度;
        var iTop = (document.documentElement.clientHeight - iHeight) / 2;
        var iLeft = (document.documentElement.clientWidth - iWidth) / 2;
        $("#scan").height(iHeight)
            .width(iWidth)
            .css('position', 'absolute')
            .css("top", iTop)
            .css("left", iLeft);
        $("#scan").show();
        $(".window-mask").show();
        mini.getbyName("FCode").focus();
        mini.getbyName("FCode").selectText();
    }

    function Close() {
        $("#scan").hide();
        $(".window-mask").hide();

    }
</script>    